//
//  FileUtils.m
//  e-nurse
//
//  Created by  on 12/19/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "FileUtils.h"

@implementation FileUtils

+ (BOOL)directoryExists:(NSString*)directory
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *tmpSubpaths = [fileManager contentsOfDirectoryAtPath:[directory stringByDeletingLastPathComponent] error:nil];
    NSString *lastcomponent = [directory lastPathComponent];
    for (NSString* content in tmpSubpaths) 
    {
        if([content isEqualToString:lastcomponent])
        {
            return  true;
        }
    }  
    
    return false;
}

+ (void)saveData:(NSData*)data filePath:(NSString*)filePath
{
    if(![FileUtils directoryExists:[filePath stringByDeletingLastPathComponent]])
    {
        NSFileManager *fileManager = [NSFileManager defaultManager];
        [fileManager createDirectoryAtPath:[filePath stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:nil];        
    }
    
    NSError *error;
    [data writeToFile:filePath options:NSDataWritingFileProtectionNone error:(&error)];
}

@end
